<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-22 22:15:35
 * @LastEditTime: 2024-03-19 13:58:49
 * @FilePath: \env-manager-pc\src\views\dashboard\yunwei\xinzenghetong.vue
-->
<script setup lang="ts">
import * as Echarts from 'echarts'
import useDashboardStore from '@/store/modules/dashboard'

const dashboardStore = useDashboardStore()

const option = computed(() => {
  const data: any[] = []
  Object.keys(dashboardStore.contractDashboard?.AddedCount || {}).forEach((key: any) => {
    data.push({
      month: key,
      value: dashboardStore.contractDashboard?.AddedCount[key],
    })
  })

  return {
    grid: {
      left: 0,
      right: '5%',
      bottom: 0,
      top: 40,
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis', // axis , item
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: data.map((item: any) => item.month),
    },
    yAxis: {
      name: '单位：个',
      type: 'value',
    },
    series: [
      {
        data: data.map((item: any) => item.value),
        type: 'line',
        areaStyle: {},
      },
    ],
  }
})

const chartRef = ref<any>()
let chart: any = null
onMounted(() => {
  chart = Echarts.init(chartRef.value)
  chart.setOption(option.value)
  window.addEventListener('resize', () => {
    chart.resize()
  })
})

dashboardStore.$subscribe((mutation, state) => {
  if (state.contractDashboard?.AddedCount) {
    chart.setOption(option.value)
  }
})
</script>

<template>
  <PageMain title="最近12月新增合同情况">
    <div ref="chartRef" class="h-[30vh] min-h-[300px] w-full overflow-hidden" />
  </PageMain>
</template>
